package codeRandomThoughts.Test322零钱兑换;

public class Solution2 {
    public static int coinChange(int[] coins, int amount) {
        int max = Integer.MAX_VALUE;
        //dp[j]:前0-i的coins凑成总金额j所需的最少硬币个数
        int[] dp = new int[amount + 1];
        for (int i = 0; i < dp.length; i++) {
            dp[i] = max;
        }

        dp[0] = 0;

        for (int i = 0; i < coins.length; i++) {
            for (int j = coins[i]; j <= amount; j++) {
                int num = dp[j - coins[i]];
                if (num != max) {
                    dp[j] = Math.min(num + 1, dp[j]);
                }
            }
        }

        return dp[amount] == max ? -1 : dp[amount];
    }
}
